package com.maxd.controller;

import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.bean.order.PartnerOrderDetail;
import com.maxd.bean.order.PartnerOrderUser;
import com.maxd.bean.partner.AddPartnerReq;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.OrderPartnerInfo;
import com.maxd.service.partner.IPartnerService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value="合伙人管理", tags = {"合伙人管理"})
@RequestMapping(value = "/partner")
public class PartnerController {

    @Autowired
    private IPartnerService partnerService;

    @GetMapping("/partner-page")
    @ApiOperation("内管分页查询合伙人列表")
    public Result<PageRes<PartnerOrderUser>> partnerPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                         @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                         @ApiParam("下单人用户ID") @RequestParam(value = "userId", required = false) Long userId,
                                                         @ApiParam("合伙人商品ID") @RequestParam(value = "partnerCode", required = false) Long partnerCode,
                                                         @ApiParam("姓名，支持模糊查询") @RequestParam(value = "userName", required = false) String userName,
                                                         @ApiParam("手机号") @RequestParam(value = "telephone", required = false) String telephone,
                                                         @ApiParam("合伙人用户ID") @RequestParam(value = "partnerUserId", required = false) Long partnerUserId,
                                                         @ApiParam("合伙人分类ID") @RequestParam(value = "partnerTypeId", required = false) Long partnerTypeId) {
        AdminUtils.checkLogin();
        PageRes<PartnerOrderUser> pageRes = partnerService.queryList(pageNo, pageSize, userId, partnerCode, userName, telephone, partnerUserId, partnerTypeId);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/partner-list")
    @ApiOperation("内管查询合伙人列表")
    public Result<List<OrderPartnerInfo>> partnerList(@ApiParam("下单人用户ID") @RequestParam(value = "userId", required = false) Long userId,
                                                      @ApiParam("合伙人商品ID") @RequestParam(value = "partnerCode", required = false) Long partnerCode,
                                                      @ApiParam("姓名，支持模糊查询") @RequestParam(value = "userName", required = false) String userName,
                                                      @ApiParam("手机号") @RequestParam(value = "telephone", required = false) String telephone,
                                                      @ApiParam("合伙人用户ID") @RequestParam(value = "partnerUserId", required = false) Long partnerUserId,
                                                      @ApiParam("合伙人分类ID") @RequestParam(value = "partnerTypeId", required = false) Long partnerTypeId) {
        AdminUtils.checkLogin();
        List<OrderPartnerInfo> pageRes = partnerService.queryList(userId, partnerCode, userName, telephone, partnerUserId, partnerTypeId);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/my-partner-info")
    @ApiOperation("商城查询我的合伙人信息")
    public Result<List<OrderPartnerInfo>> myPartnerInfo(@ApiParam("合伙人分类ID") @RequestParam(value = "partnerTypeId", required = false) Long partnerTypeId) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        List<OrderPartnerInfo> pageRes = partnerService.queryUserPartnerList(userId, partnerTypeId);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/my-partner-orders")
    @ApiOperation("商城查询我的合伙人订单列表")
    public Result<List<PartnerOrderDetail>> myPartnerOrders(@ApiParam("合伙人分类ID") @RequestParam(value = "partnerTypeId", required = false) Long partnerTypeId) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        List<PartnerOrderDetail> details = partnerService.queryUserPartnerDetails(userId, partnerTypeId);
        return ResultUtil.success(details);
    }

    @PostMapping("/add-partner")
    @ApiOperation("内管添加合伙人")
    public Result<Void> addPartner(@RequestBody @Validated AddPartnerReq req) {
        AdminUtils.checkLogin();
        partnerService.addPartner(req);
        return ResultUtil.success();
    }

}
